/*
Learning from Law Enforcement
Libor Dusek and Christian Traxler

Do-file producing regression results and figures in the paper and (online) appendix

*************************************
*** DESCRIPTION AND PRELIMINARIES
**************************************

*** This do file: RD_03.do
    Runs sensitivity analyses wrt parameters a and f (assignment / follow-up period) at the ENFORCEMENT cutoff

*** Required packages:
    coefplot , grstyle , rdrobust

*** The input dataset:
    Dusek_Traxler_RD_a3.dta
    Dusek_Traxler_RD_a4.dta
    Dusek_Traxler_RD_a5.dta
    Dusek_Traxler_RD_a6.dta
    	
*** Output is stored in output directories: OUTPUT/RD_TAB  OUTPUT/RD_FIG , and OUTPUT/RD_temp
	Names of outpupt files correspond to the Table/Figure numbers in the paper and (online) appendix
	
*** List of Figures and Tables produced:
    => Figure A.9 and Tables A.8 and A.9
    See also Section C of the ReadMe File.
*/

cap clear
set more off

/* Set directories */
global dir C:/yourdir/
cd    $dir
global datadir $dir/datasets/
global outputdir outfiles/
capture mkdir ${outputdir}
capture mkdir ${outputdir}/RD_TAB
capture mkdir ${outputdir}/RD_FIG
capture mkdir ${outputdir}/RD_temp

cap log close
log using    $dir/${outputdir}/log_RD_03.txt, replace

cap erase ${outputdir}/RD_TAB/Tab_A8_speedm.tex
cap erase ${outputdir}/RD_TAB/Tab_A8_speedm.txt
cap erase ${outputdir}/RD_TAB/Tab_A9_speeding.tex
cap erase ${outputdir}/RD_TAB/Tab_A9_speeding.txt

/* Load data with different Assignment Periods A (in months) */
forvalues a = 3(1)6 {

   global A = `a'
    di "assignment period (months):"
    di `a'

  /* Within each dta file, pick a follow-up (outcome) period 3<=f<=6 */
  forvalues f = 3(1)6 {
     di "outcome period (months):"
     di `f'
     global F = `f'

     cap clear
     use $datadir/Dusek_Traxler_RD_a${A}.dta

     rename period_follup_${F} period_follup
     drop period_follup_*
     rename period_follup period_post
     rename dum_haspost_${F} dum_haspost
     drop dum_haspost_*
     rename period_pre_${F} period_pre
     drop period_pre_*

     /* Only keep observations from pre & outcome period */
     qui keep if (period_pre==1 | period_post==1 | dum_trigger==1)

     /* only defined in A=4 data; other data are already cut */
     keep if day_trigger<td(1jul2017)

     /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * I.A   DEFINE VARIABLES AND SAMPLE  [compare RD_01.do]
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

     /* Defines assignment speed (cutoff 1) */
     gen speed_Z = maxspeed - 14

     /* focus on sample around Cutoff 1 */
     qui keep if speed_Z>-14 & speed_Z<9

     /* define first relevant episode (after sample restrictions) and cut sample */
     gegen epi_min=min(episode), by(id)
     keep if episode==epi_min
     drop epi_min

     /* Identifier for first relevant drive through in pre period */
     egen xxid_record_pre_min  = min(id_record) if (period_pre==1 | dum_trigger==1), by(id)
     egen id_record_pre_min  = mean(xxid_record_pre_min), by(id)
     drop xxid_record_*

     /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * I.B   PRIMARY OUTCOME AND TREATMENT VARIABLES
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

     /*TREATMENT RATE (1): share of rides that happened after being ticketed */
     gegen xxticketed_m = mean(dum_ticketed) if period_post==1, by(id)
     gegen ticketed_m = mean(xxticketed_m), by(id)
     drop xxticketed*

     /*AVERAGE SPEED  (outcome var) */
     gegen xxspeedm_m    = mean(speedm) if period_post==1, by(id)
     gegen speedm_m    = mean(xxspeedm_m), by(id)
     drop xxspeedm_*

     /*SPEEDING RATE (outcome var), share of rides above speed limit */
     gegen xxspeeding_m     = mean(speeding) if period_post==1, by(id)
     gegen speeding_m     = mean(xxspeeding_m), by(id)
     drop xxspeeding_*

     /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * I.C   WALD ESTIMATES (car level), Cutoff 1
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
     gen     ESTSAMPLE=0
     replace ESTSAMPLE=1 if id_record==id_record_pre_min & dum_haspost==1

     /* Output for producing Table A8 */
      rdrobust speedm_m speed_Z if ESTSAMPLE==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
       estimates store speedm_a${A}_${F}
        local bw = h_mserd
        qui su speedm_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
        local y_l = r(mean)
        local rel = 100 * e(tau_bc)/`y_l'
       outreg2 using ${outputdir}/RD_TAB/Tab_A8_speedm.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(F = ${F}) append

     /* Output for producing Table A9 */
      rdrobust speeding_m  speed_Z if ESTSAMPLE==1, fuzzy(ticketed_m) all p(1) bwselect(mserd) vce(hc0) kernel(tri)
       estimates store speeding_a${A}_${F}
       local bw = h_mserd
        qui su speeding_m if speed_Z<0 & speed_Z>-0.5 & ESTSAMPLE==1
        local y_l = r(mean)
        local rel = 100 * e(tau_bc)/`y_l'
       outreg2 using ${outputdir}/RD_TAB/Tab_A9_speeding.tex, tex(frag) se br addstat(Y_L, `y_l', Effect, `rel', Bandw, `bw') adec(3) sdec(4) bdec(4) ctitle(F = ${F}) append
   }
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* I.D   Coefficient Plots => Figure A.9, (a) + (c)
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

grstyle init
grstyle color background white

grstyle graphsize x 7
grstyle graphsize y 4

coefplot (speeding_a3_3, label(f = 3) pstyle(p3)) (speeding_a3_4, label(f = 4) pstyle(p4)) (speeding_a3_5, label(f = 5) pstyle(p5)) (speeding_a3_6, label(f = 6) pstyle(p6)), bylabel("a = 3") ///
      || (speeding_a4_3, label(f = 3) pstyle(p3)) (speeding_a4_4, label(f = 4) pstyle(p4)) (speeding_a4_5, label(f = 5) pstyle(p5)) (speeding_a4_6, label(f = 6) pstyle(p6)), bylabel("a = 4") ///
      || (speeding_a5_3, label(f = 3) pstyle(p3)) (speeding_a5_4, label(f = 4) pstyle(p4)) (speeding_a5_5, label(f = 5) pstyle(p5)) (speeding_a5_6, label(f = 6) pstyle(p6)), bylabel("a = 5") ///
      || (speeding_a6_3, label(f = 3) pstyle(p3)) (speeding_a6_4, label(f = 4) pstyle(p4)) (speeding_a6_5, label(f = 5) pstyle(p5)) (speeding_a6_6, label(f = 6) pstyle(p6)), bylabel("a = 6") ///
      || , drop(Conventional Bias-corrected) vert msymbol(D) rename(Robust = beta) byopts(compact cols(4)) subtitle(, size(medium)) legend(rows(1) size(medium) region(fcolor(none) lw(zero) lcolor(gs16)))
 graph export ${outputdir}/RD_FIG/Fig_A9_a_speeding.pdf, replace as(pdf)
 graph save   ${outputdir}/RD_FIG/Fig_A9_a_speeding.gph, replace


coefplot (speedm_a3_3, label(f = 3) pstyle(p3)) (speedm_a3_4, label(f = 4) pstyle(p4)) (speedm_a3_5, label(f = 5) pstyle(p5)) (speedm_a3_6, label(f = 6) pstyle(p6)), bylabel("a = 3") ///
      || (speedm_a4_3, label(f = 3) pstyle(p3)) (speedm_a4_4, label(f = 4) pstyle(p4)) (speedm_a4_5, label(f = 5) pstyle(p5)) (speedm_a4_6, label(f = 6) pstyle(p6)), bylabel("a = 4") ///
      || (speedm_a5_3, label(f = 3) pstyle(p3)) (speedm_a5_4, label(f = 4) pstyle(p4)) (speedm_a5_5, label(f = 5) pstyle(p5)) (speedm_a5_6, label(f = 6) pstyle(p6)), bylabel("a = 5") ///
      || (speedm_a6_3, label(f = 3) pstyle(p3)) (speedm_a6_4, label(f = 4) pstyle(p4)) (speedm_a6_5, label(f = 5) pstyle(p5)) (speedm_a6_6, label(f = 6) pstyle(p6)), bylabel("a = 6") ///
      || , drop(Conventional Bias-corrected) vert msymbol(D) rename(Robust = beta) byopts(compact cols(4)) subtitle(, size(medium)) legend(rows(1) size(medium) region(fcolor(none) lw(zero) lcolor(gs16)))
 graph export ${outputdir}/RD_FIG/Fig_A9_c_speed.pdf, replace as(pdf)
 graph save   ${outputdir}/RD_FIG/Fig_A9_c_speed.gph, replace


log close
